home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / DESKLIBC / SOURCES.ZIP / DeskLib / !DLSources / Libraries / Filing / c / ScanDir < prev    next >
Text File  |  1995-08-27  |  3KB  |  109 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Filing.ScanDir.c
  12.     Author:  Copyright © 1995 Sergio Monesi
  13.     Version: 1.01 (27 Aug 1995)
  14.     Purpose: Scans a directory tree calling the specified functions.
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "DeskLib:Core.h"
  20. #include "DeskLib:Filing.h"
  21.  
  22. #define BUF__SIZE 2048      /* size of the buffer where the directory entries are read every time */
  23.  
  24. typedef struct {
  25.   Filing_ScanDir_StartDir *startdir;
  26.   Filing_ScanDir_FoundFile *foundfile;
  27.   Filing_ScanDir_EndDir *enddir;
  28. } scandir_funcs;
  29.  
  30. static os_error *Filing__ScanDirRec(char *dirname, filing_fulldirentry *dirdata, scandir_funcs *funcs)
  31. {
  32.  int readnum=0, offset=0;
  33.  char direntries[BUF__SIZE];
  34.  os_error *er;
  35.  filing_fulldirentry *actptr;
  36.  
  37.  if (funcs->startdir!=NULL) {
  38.    if (er=funcs->startdir(dirname,dirdata),er!=NULL)
  39.      return er;
  40.  }
  41.  
  42.  do {
  43.    if (readnum==0) {
  44.      if (offset==-1) {
  45.        /* no more objects to read */
  46.        break;
  47.      }
  48.      readnum=255;
  49.      er=Filing_ReadFullDirEntry(dirname,(filing_fulldirentry *)direntries,&readnum,&offset,BUF__SIZE,NULL);
  50.      if (er!=NULL) {
  51.        return er;
  52.      }
  53.      if (readnum==0) {
  54.        break;
  55.      }
  56.      actptr=(filing_fulldirentry *)direntries;
  57.    }
  58.    else {
  59.      actptr=(filing_fulldirentry *)(((int)(&actptr->name)+strlen(actptr->name)+4)&~3);
  60.    }
  61.    readnum--;
  62.  
  63.    if (funcs->foundfile!=NULL) {
  64.      if (er=funcs->foundfile(dirname,actptr),er!=NULL) {
  65.        return er;
  66.      }
  67.    }
  68.  
  69.    if (actptr->objtype==2) {
  70.      char newname[256];
  71.      Filing_MakePath(newname,dirname,actptr->name);
  72.      if (er=Filing__ScanDirRec(newname,actptr,funcs),er!=NULL) {
  73.        return er;
  74.      }
  75.    }
  76.  } while(TRUE);
  77.  
  78.  if (funcs->enddir!=NULL) {
  79.    if (er=funcs->enddir(dirname,dirdata),er!=NULL)
  80.      return er;
  81.  }
  82.  
  83.  return NULL;
  84. }
  85.  
  86. os_error *Filing_ScanDir(char *dirname,
  87.                          Filing_ScanDir_StartDir *startdirproc,
  88.                          Filing_ScanDir_FoundFile *foundfileproc,
  89.                          Filing_ScanDir_EndDir *enddirproc)
  90. {
  91.  os_error *er;
  92.  scandir_funcs funcs;
  93.  
  94.  funcs.startdir=startdirproc;
  95.  funcs.foundfile=foundfileproc;
  96.  funcs.enddir=enddirproc;
  97.  
  98.  if (dirname[strlen(dirname)-1]!='$') {
  99.    filing_fulldirentry dirdata;
  100.    if (er=Filing_SingleFullDirEntry(dirname,&dirdata,sizeof(dirdata)),er!=NULL)
  101.      return er;
  102.    return Filing__ScanDirRec(dirname,&dirdata,&funcs);
  103.  }
  104.  else {
  105.    /* it could be possible to 'forge' the filing_fulldirentry for the root directory... */
  106.    return Filing__ScanDirRec(dirname,NULL,&funcs);
  107.  }
  108. }
  109.